﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Model;
using Newtonsoft.Json;

namespace OASystem.Controllers
{

    [App_Start.LoginFilterAtrribute]
    public class EmployeeController : Controller
    {

        private static EmployeeManager em = new EmployeeManager();

        public ActionResult Index(string keyword = "", int PageSize = 10, int PageNo = 1)
        {

            var empList = em.QueryPageList(PageSize, PageNo, keyword);

            return View(empList);
        }


        /// <summary>
        /// 用户详情页面
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns> 
        public ActionResult UserDetail(int id)
        {

            var account = BLL.EmployeeManager.GetUserAccount(id);
            var userBasic = BLL.EmployeeManager.GetUserBasic(id);
            ViewBag.UserAccount = account;
            ViewBag.UserBasic = userBasic;

            return View();
        }

        /// <summary>
        /// 新增页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Insert()
        {

            return View();
        }


        /// <summary>
        /// 录入员工信息
        /// </summary>
        /// <param name="Account"></param>
        /// <param name="RealName"></param>
        /// <param name="Sex"></param>
        /// <param name="Birthday"></param>
        /// <param name="IDCard"></param>
        /// <param name="Address1"></param>
        /// <param name="Address2"></param>
        /// <param name="PositionId"></param>
        /// <param name="DepartmentId"></param>
        /// <returns></returns>
        public JsonResult Add(string Account, string RealName, bool Sex, DateTime Birthday, string IDCard, string Address1, string Address2, int PositionId, int DepartmentId)
        {

            try
            {
                OA_UserAccount account = new OA_UserAccount()
                {
                    Account = Account,
                    PassWord = "123456",
                    IsEnable = true,
                    IsSuper = false,
                    InsertTime = DateTime.Now,
                    UpdateTime = DateTime.Now
                };
                OA_UserBasic userBasic = new OA_UserBasic()
                {
                    RealName = RealName,
                    Address1 = Address1,
                    Address2 = Address2,
                    Brithday = Birthday,
                    DepartmentId = DepartmentId,
                    PositionId = PositionId,
                    IDCard = IDCard,
                    Sex = Sex,
                    InsertTime = DateTime.Now,
                    UpdateTime = DateTime.Now
                };

                var result = new BLL.EmployeeManager().AddEmployee(account, userBasic);

                return Json(new { Code = 1, Data = result });
            }
            catch (Exception)
            {

                return Json(new { Code = -1 });
            }

        }


        /// <summary>
        /// 修改账号状态
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="IsEnable"></param>
        /// <returns></returns>
        public JsonResult ToggleStatus(int Id, bool IsEnable)
        {
            try
            {
                var num = new BLL.EmployeeManager().UpdateEmpStatus(Id, IsEnable);

                return Json(new { Code = 1 });
            }
            catch (Exception ex)
            {
                return Json(new { Code = -1 });
            }
        }

        /// <summary>
        /// 数据导入Excel
        /// </summary>
        /// <returns></returns>
        public JsonResult DataToExcel()
        {

            var result = em.EmployeeToExcel();
            return Json(result);

        }



    }
}
